package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduCourseMapper;
import com.zenith.edu.po.domain.EduCourse;
import com.zenith.edu.po.dto.EduCourseDTO;
import com.zenith.edu.po.vo.EduCourseVO;
import com.zenith.edu.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    
    @Override
    public EduCourseVO createCourse(EduCourseDTO dto) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(dto, course);
        this.save(course);
        
        EduCourseVO vo = new EduCourseVO();
        BeanUtils.copyProperties(course, vo);
        return vo;
    }
    
    @Override
    public EduCourseVO getCourseById(Long id) {
        EduCourse course = this.getById(id);
        if (course == null) {
            return null;
        }
        
        EduCourseVO vo = new EduCourseVO();
        BeanUtils.copyProperties(course, vo);
        return vo;
    }
    
    @Override
    public EduCourseVO updateCourse(EduCourseDTO dto) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(dto, course);
        this.updateById(course);
        
        EduCourseVO vo = new EduCourseVO();
        BeanUtils.copyProperties(course, vo);
        return vo;
    }
    
    @Override
    public boolean deleteCourse(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduCourseVO> getCourseList() {
        List<EduCourse> courseList = this.list();
        return courseList.stream().map(course -> {
            EduCourseVO vo = new EduCourseVO();
            BeanUtils.copyProperties(course, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}